//
//  StageControl.m
//  storeapp
//
//  Created by huangyi on 12-11-27.
//  Copyright (c) 2012年 ios. All rights reserved.
//

#import "StageMgr.h"
#import "CViewController.h"
#import "toolbox.h"

@interface StageMgr ()

@end

@implementation StageMgr

+(StageMgr*)shared{
    static StageMgr *singleton=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton=[[StageMgr alloc] init];
    });
    return singleton;
}

-(id)init{
    self=[super init];
    if (self) {
        self.rootStage=[[[UIApplication sharedApplication] windows] objectAtIndex:0];
        self.states=[NSMutableArray array];
        
        if (DEBUGDRAW) {
            [self debugInspector];
        }
    }
    return self;
}

-(void)debugInspector{
    int64_t delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSArray *windows=[[UIApplication sharedApplication] windows];
        for (UIWindow *window in windows) {
            [window inspector];
        }
        [self debugInspector];
    });
}

-(void)loadStage:(NSString*)format,...{
    NSString *params=nil;
	va_list args;
	if (format)
	{
		va_start(args, format);
        params=[[NSString alloc] initWithFormat:format arguments:args];
		va_end(args);
	}
    CViewController *load=nil;
    CViewController *unload=nil;
    if ([self.rootStage.rootViewController isKindOfClass:[CViewController class]]) {
        unload=(id)self.rootStage.rootViewController;
    }
    if (params) {
        NSArray *match=[params regexp:@"(?<=storeapp://)\\w+"];
        NSString *scheme=match.firstObject;
        if (scheme) {
            Class controllerclass=NSClassFromString(scheme);
            if ([controllerclass isSubclassOfClass:[CViewController class]]) {
                load=[[controllerclass alloc] init];
                load.args=params;
            }
        }
    }
    if (load) {
        [unload pop];
        [self animation];
        self.rootStage.rootViewController=load;
        dispatch_async(dispatch_get_main_queue(), ^{
            [load push];
        });
        DDLogCVerbose(@"stage:%@",load.description);
    }else{
        NSString *str=[NSString stringWithFormat:@"notsupport,%@",params];
        SendAlert(str);
    }
}

-(id)stageViewController{
    return self.rootStage.rootViewController;
}

-(void)pushState:(NSString*)state{
    [self.states addObject:state];
    int max_state=1000;
    while (self.states.count>max_state) {
        [self.states removeFirstObject];
    }
}

-(NSString*)popState{
    NSString *state=self.states.lastObject;
    [self.states removeLastObject];
    return state;
}
-(void)clearState{
    [self.states removeAllObjects];
}

-(void)animation{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.25;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionFade;
    [[self.rootStage layer] addAnimation:animation forKey:@"animation"];
}

-(NSString*)description{
    return self.states.description;
}

@end

